package oh.june.calleridentifier.module;

import java.util.List;

import oh.june.calleridentifier.R;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class ListCustomAdapter extends ArrayAdapter<DataRow>{

	private static int LAYOUT = R.layout.list_item;
	private LayoutInflater mInflater;
	private OnClickListener _listener;
	private boolean mCheckBoxShow;
	
	public ListCustomAdapter(Context context, List<DataRow> objects) {
		super(context, LAYOUT, objects);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if(v == null)
			v = mInflater.inflate(LAYOUT, null);
		
		TextView tvName = (TextView)v.findViewById(R.id.name);
		TextView tvMemo = (TextView)v.findViewById(R.id.memo);
		TextView tvNumber = (TextView)v.findViewById(R.id.tel);
		TextView tvInsDt = (TextView)v.findViewById(R.id.insdt);
		Button btn = (Button)v.findViewById(R.id.btn_toNext);
		btn.setOnClickListener(_listener);
		
		final CheckBox chkDel = (CheckBox)v.findViewById(R.id.chk_del);
		if(mCheckBoxShow){
			chkDel.setVisibility(View.VISIBLE);
		}else{
			chkDel.setVisibility(View.INVISIBLE);
		}
		
		btn.setTag(position);
		v.setTag(position);
		
		final DataRow bean = getItem(position);
		tvName.setText(bean.getString("NAME", ""));
		tvMemo.setText(bean.getString("MEMO", ""));
		tvNumber.setText(bean.getString("NUMBER", ""));
		try{
			tvInsDt.setText(DateUtil.toPatternedDate("yyyy.MM.dd HH:mm:ss", "yyyyMMddHHmmss", bean.getString("INS_DT", "")));						
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(position%2==0){
			tvInsDt.setTextColor(Color.RED);
			v.setBackgroundColor(Color.rgb(240, 240, 240));
		}else{
			tvInsDt.setTextColor(Color.BLUE);
			v.setBackgroundColor(Color.rgb(220, 220, 220));
		}
		
		boolean chk = (Boolean) bean.getTag("CHK", false);
		chkDel.setChecked(chk);
		
		chkDel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(chkDel.isChecked()){
					bean.setTag("CHK", true);
				}else{
					bean.setTag("CHK", false);
				}
			}
		});
		
		v.setOnClickListener(_listener);
		
		v.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				switch(action){
				case MotionEvent.ACTION_DOWN:
					v.setAlpha(0.7f);
					break;
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_OUTSIDE:
					v.setAlpha(1.0f);
					break;
				}
				return false;
			}
		});
		
		return v;
	}
	
	public DataTable getCheckedItems(){
		DataTable chkedList = new DataTable();
		for(int i=0; i<this.getCount(); i++){
			DataRow row = this.getItem(i);
			boolean chk = (Boolean)row.getTag("CHK", false);
			if(!chk)
				continue;
			chkedList.add(row);
		}
		return chkedList;
	}
	
	public void setOnBtnClickListener(OnClickListener onClickListener) {
		_listener = onClickListener;
	}
	
	public void setCheckboxShow(boolean b){
		for(int i=0; i<this.getCount(); i++){
			DataRow row = this.getItem(i);
			row.setTag("CHK", false);
		}
		
		mCheckBoxShow = b;
		notifyDataSetChanged();
	}
}
